﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinqToTwitter;

namespace Twilter
{

    // gloaablne klass usernime, paroolide jms hoidmiseks.
    static class GlobalInfo
    {
        private static string twitterUser = "";
        private static string twitterPass = "";
        private static TwitterContext twitterCtx;
        private static LinqBackend linqbackendCtx;

        public static string TwitterUsername
        {
            get { return twitterUser; }
            set { twitterUser = value; }
        }
        public static string TwitterPassword
        {
            get { return twitterPass; }
            set { twitterPass = value; }
        }
        public static TwitterContext TwitterCtx
        {
            get { return twitterCtx; }
            set { twitterCtx = value; }
        }
        
        public static LinqBackend LinqBackendCtx
        {
            get { return linqbackendCtx; }
            set { linqbackendCtx = value; }
        }


    }

   class LinqBackend
    {
       
   

        public void SignIn () {
           
           UsernamePasswordAuthorization auth = new UsernamePasswordAuthorization(null);
            
          auth.UserName = GlobalInfo.TwitterUsername;
          auth.Password = GlobalInfo.TwitterPassword;            
          var twitterCtx = new TwitterContext(auth, "https://twitter.com/", "http://search.twitter.com/");
          GlobalInfo.TwitterCtx = twitterCtx;

          if (string.IsNullOrEmpty(auth.UserName) || string.IsNullOrEmpty(auth.Password))
          {
              throw new Exception();
          }
          try
          {
              auth.SignOn();
          }
          catch (Exception)
          {
              throw;
          }
       
         
         //UpdateStatusDemo(twitterCtx);
            
        }
        public IQueryable <Status> GetPublicTimeline(TwitterContext twitterCtx)
        {
           
            var publicTweets =
                from tweet in twitterCtx.Status
                where tweet.Type == StatusType.Public
                select tweet;
            //twitterCtx.
            return publicTweets;
            //publicTweets.ToList().ForEach(
            //    tweet => Console.WriteLine(
            //        "User Name: {0}, Tweet: {1}",
            //        tweet.User.Name,
            //        tweet.Text));
        }
    public void UpdateStatusDemo(TwitterContext twitterCtx)
        {
    
            var status = "\u00C7 Testing LINQ to Twitter update status on " + DateTime.Now.ToString() + " #linqtotwitter";

            Console.WriteLine("Status being sent: " + status);

            var tweet = twitterCtx.UpdateStatus(status);

            Console.WriteLine(
                "Status returned: " +
                "(" + tweet.ID + ")" +
                "[" + tweet.User.ID + "]" +
                tweet.User.Name + ", " +
                tweet.Text + ", " +
                tweet.CreatedAt);
        }
    }
}
